package com.bosssoft.springcloud.controller;

import com.bosssoft.springcloud.entity.dto.RoleDTO;
import com.bosssoft.springcloud.service.RoleService;
import com.bosssoft.springcloud.entity.vo.CommenResult;
import com.bosssoft.springcloud.entity.vo.RoleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class RoleController {
    @Autowired
    private RoleService roleService;

    /***
     * 获取角色信息
     * @param roleVO
     * @return
     */
    @GetMapping("/rbac/role/getRole")
     public CommenResult getRole(@RequestBody RoleVO roleVO){
         RoleDTO roleDTO = new RoleDTO();
         BeanUtils.copyProperties(roleDTO,roleVO);
         RoleDTO role = roleService.findRoleById(roleDTO.getRid());
         if (role!=null){
             return new CommenResult(200,"查询成功",role.toString());
         }
         return new CommenResult(404,"查询失败",null);
     }

    /***
     * 添加角色
     * @param roleVO
     * @return
     */
    @GetMapping("/rbac/role/addRole")
    public CommenResult addRole(@RequestBody RoleVO roleVO){
        RoleDTO roleDTO = new RoleDTO();
        BeanUtils.copyProperties(roleDTO,roleVO);
        int res = roleService.addRole(roleDTO);
        if (res > 0){
            return new CommenResult(200,"添加成功",res);
        }
        return new CommenResult(404,"添加失败",res);
    }

    /***
     * 移除角色
     * @param roleVO
     * @return
     */
    @GetMapping("/rbac/role/removeRole")
    public CommenResult removeRole(@RequestBody RoleVO roleVO){
        RoleDTO roleDTO = new RoleDTO();
        BeanUtils.copyProperties(roleDTO,roleVO);
        int res = roleService.removeRole(roleDTO.getRid());
        if (res > 0){
            return new CommenResult(200,"移除成功",res);
        }
        return new CommenResult(404,"移除失败",res);
    }

    /***
     * 为角色添加权限
     * @param roleVO
     * @param permissionsID
     * @return
     */
    @GetMapping("/rbac/role/addRolePermission")
    public CommenResult addRolePermissions(@RequestBody RoleVO roleVO, List<Integer> permissionsID){
        RoleDTO roleDTO = new RoleDTO();
        BeanUtils.copyProperties(roleDTO,roleVO);
        List<Integer> NowPermissionsId = roleDTO.getPermissionsId();
        boolean b = NowPermissionsId.addAll(permissionsID);
        if (b){
            return new CommenResult(200,"添加成功",b);
        }
        return new CommenResult(404,"添加失败",b);
    }

}
